package com.woniuxy.JSP;

import com.woniuxy.Bean.Test;
import com.woniuxy.Mapper.TestMapper;
import com.woniuxy.Util.sqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @auther linke
 * @create 2021-10-20 19:10
 */
@WebServlet("/jspDemo02")
public class JspDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SqlSession sqlSession = null;
        try {
            sqlSession = sqlSessionUtil.sqlSession();
        } catch (Exception e) {
            e.printStackTrace();
        }
        TestMapper tm = sqlSession.getMapper(TestMapper.class);
        final List<Test> list = tm.findByAll();
        System.out.println(list);
        ServletContext servletContext = req.getServletContext();
        servletContext.setAttribute("list",list);
        /*PrintWriter pw = resp.getWriter();
        for (Test test : list) {
            pw.write(test.toString());
        }*/
        resp.sendRedirect("/web03/JspDemo12.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
